global class InsertHospitalChangeHistoryClass{
    
    /*==============================================
    2011-01-7 created by lisiheng:
        fix too many DML and too many SOQL problem and ... some other limit...
        
    ===============================================*/
    
    @future
    public static void RunMethod(list<id> hidl){
        list<hospital__c> hosl=[select id,owner.lastname,owner.firstname,effectivedate__c,Name,class__c   from hospital__c where id in:hidl];
        list<hospital_approve__c> hal=[select hospital__c,id,Name, approvalstatus__c,owner__c, class__c, approvaltype__c, Active__c, 
                                classchangeto__c, ownerchangeto__c,ownerchangeto__r.firstname,ownerchangeto__r.lastname, namechangeto__c, comments__c,hospital__r.comments__c,hospital__r.effectivedate__c
                                 from Hospital_approve__c 
                                where hospital__c in:hidl];
                                
        map<id,hospital_approve__c>  hamap=new map<id,hospital_approve__c>();     
        map<id,hospital_approve__c> deleteHCPActivity=new map<id,hospital_approve__c>();
        for(hospital_approve__c ha:hal){
            hamap.put(ha.hospital__c,ha);
            if(ha.approvaltype__c == '删除医院' || ha.approvaltype__c == '改变MSR'){
                deletehcpactivity.put(ha.hospital__c,ha);
            }
        }
        if(deletehcpactivity.size()>0){
            //处理更改的医院对应的HCP和活动；
            list<hcp__c> hcpl=[select id,active__c,hospital__c from hcp__c where hospital__C in:deletehcpactivity.keyset()];
            list<HCPChangeHistory__c> insHCPHistory=new list<HCPChangeHistory__c>();
            for(hcp__c h:hcpl){
                if(deletehcpactivity.get(h.hospital__c).approvaltype__c== '删除医院'){
                    HCPChangeHistory__c HC = new HCPChangeHistory__c (HCP__c=h.Id, 
                                                                        Name = '删除医院' ,
                                                                        Old__c='',
                                                                        New__c='',
                                                                        comments__c=deletehcpactivity.get(h.hospital__c).hospital__r.comments__c,
                                                                        effectivedate__c=deletehcpactivity.get(h.hospital__c).hospital__r.effectivedate__c);
                     inshcphistory.add(hc);
                 }
            }
            if(inshcphistory.size()>0){
                insert inshcphistory;
            }

            list<id> rtidl=new list<id>();
            list<recordtype> rt=[select id from recordtype where (name=:'科内会' and sobjecttype=:'hcpeducation__c') or (name=:'妈妈班' and sobjecttype=:'consumereducation__c') ];
                for(recordtype rtid:rt){
                    rtidl.add(rtid.id);
                }
            list<hcpeducation__c> hcpedul=[select id from hcpeducation__c where (Hospital__c in:deletehcpactivity.keyset() or OrganigerHospital__c in:deletehcpactivity.keyset()) and recordtypeid in:rtidl and date__c>=:date.today()];
                if(hcpedul.size()>0){
                    for(HCPeducation__c hcpedu:hcpedul){
                        hcpedu.deletemark__c=true;
                    }
                    update hcpedul;
                    delete hcpedul;
                }
            list<ConsumerEducation__c> csedul=[select id from ConsumerEducation__c where (Hospital__c in:deletehcpactivity.keyset() or OrganigerHospital__c in:deletehcpactivity.keyset()) and recordtypeid in:rtidl and date__c>=:date.today()];
                if(csedul.size()>0){
                    for(ConsumerEducation__c csedu:csedul){
                        csedu.deletemark__c=true;
                    }
                    update csedul;
                    delete csedul;
                }
            list<callplan__c> delCallplan=[select id from callplan__c where IsDeleted =: false and CallPlanDate__c>=:date.today() and hcp__r.hospital__c in:deletehcpactivity.keyset()];
            if(delcallplan.size()>0){
                for(callplan__c cp:delcallplan){
                    cp.delete_mark__c=true;
                }
                update delcallplan;
                delete delcallplan;
            }                
            
        }
        
        list<hospital__c> updatehospital=new list<hospital__c>();
        list<HospitalChangeHistory__c> insertHistory=new list<HospitalChangeHistory__c >();                        
        hospital_approve__c ha;

        List<String> HosOwnerIds = new List<String>();
        List<String> HosChangeOwnerIds = new List<String>();
        for(hospital__c hos:hosl)
        {
            ha=hamap.get(hos.id);
            HosOwnerIds.add(hos.ownerid);
            HosChangeOwnerIds.add(hos.ownerchangeto__c);
        }
        List<user> useroldList=[select lastname,firstname,id from user where id in: HosOwnerIds];
        List<user> usernewList=[select lastname,firstname,id from user where id in: HosChangeOwnerIds];
        Map<String,User> useroldMap = new Map<String,User>();
        Map<String,User> usernewMap = new Map<String,User>();
        for(user u : useroldList)
        {
            useroldMap.put(u.id, u);
        }
        for(user u : usernewList)
        {
            usernewMap.put(u.id, u);
        }        
                        
        for(hospital__c hos:hosl){
        //=====================
             ha=hamap.get(hos.id);
            if(ha.approvaltype__c == '新增医院'){
                    HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                                   Name = ha.approvaltype__c,
                                                   Old__c='',
                                                   New__c='',
                                                   comments__c=ha.comments__c,
                                                   effectivedate__c=hos.effectivedate__c);
                
                    hos.Active__c = true;
                    insertHistory.add(hch);
             }
             else if(ha.approvaltype__c==''||ha.approvaltype__c==null){
                  HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                               Name = '重新激活医院',
                                               Old__c='',
                                               New__c='',
                                               comments__c=ha.comments__c,
                                               effectivedate__c=hos.effectivedate__c);
                  hos.Active__c = true;
                  insertHistory.add(hch);
            }
            else if(ha.approvaltype__c == '删除医院'){
               HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id,
                                               Name = ha.approvaltype__c,
                                               Old__c='',
                                               New__c='',
                                               comments__c=ha.comments__c,
                                               effectivedate__c=hos.effectivedate__c);
                hos.Active__c = false;
                insertHistory.add(hch);
            }
            else if(ha.approvaltype__c == '医院重命名'){
                HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (
                                                Hospital__c=hos.Id, 
                                               Name = ha.approvaltype__c,
                                               Old__c=hos.Name,
                                               New__c=ha.namechangeto__c,
                                               comments__c=ha.comments__c,
                                               effectivedate__c=hos.effectivedate__c);
                hos.Name = ha.namechangeto__c;
                insertHistory.add(hch);
            }
            else if(ha.approvaltype__c == '改变医院级别'){
                HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                               Name = ha.approvaltype__c,
                                               Old__c=hos.class__c,
                                               New__c=ha.classchangeto__c,
                                               comments__c=ha.comments__c,
                                               effectivedate__c=hos.effectivedate__c);
                hos.class__c = ha.classchangeto__c;
                insertHistory.add(hch);
            }
             else if(ha.approvaltype__c == '改变MSR'){

                user userold= useroldMap.get(hos.ownerid);
                user usernew= usernewMap.get(ha.ownerchangeto__c);

                HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=hos.Id, 
                                               Name = ha.approvaltype__c,
                                               Old__c=hos.owner.lastname+hos.owner.firstname,
                                               New__c=ha.ownerchangeto__r.lastname+ha.ownerchangeto__r.firstname,
                                               comments__c=ha.comments__c,
                                               effectivedate__c=hos.effectivedate__c);
                hos.OwnerId = ha.ownerchangeto__c;
                insertHistory.add(hch);
            }
            hos.approvaltype__c = null;
            hos.effectivedate__c=null;
            ha.approvaltype__c = null;
            ha.classchangeto__c = null;
            ha.namechangeto__c = null;
            ha.ownerchangeto__c = null;
            ha.effectivedate__c = null;
            ha.comments__c = null;
            ha.approvalstatus__c=null;
            hos.approvalstatus__c=null;
            hos.comments__c=null;
            
            hamap.put(hos.id,ha);
        //=====================
        }
        insert inserthistory;
        update hosl;
        update hamap.values();
        
    }
    
    @future
    public static void RejectMethod(list<id> hidl){
        list<hospital__c> hosl=[select id,effectivedate__c,comments__c,approvaltype__c  from hospital__c where id in:hidl];
        if(hosl.size()>0){
            list<HospitalChangeHistory__c> inslist=new list<HospitalChangeHistory__c>();
            for(hospital__c h:hosl){
                HospitalChangeHistory__c HCH = new HospitalChangeHistory__c (Hospital__c=h.Id, 
                                               Name = '审批拒绝:'+h.approvaltype__c,
                                               comments__c=h.comments__c,
                                               effectivedate__c=date.today());
                inslist.add(hch);
                h.effectivedate__c = null;
                h.comments__c=null;
                h.approvalstatus__c=null;
                h.approvaltype__c=null;
            }
            update hosl;
            insert inslist;
        }
        list<hospital_approve__c> hal=[select id,classchangeto__c,namechangeto__c,ownerchangeto__c,effectivedate__c,comments__c from hospital_approve__c where hospital__c in:hidl ];
        if(hal.size()>0){
            for(hospital_approve__c ha:hal){
                ha.classchangeto__c = null;
                ha.namechangeto__c = null;
                ha.ownerchangeto__c = null;
                ha.effectivedate__c = null;
                ha.comments__c = null;
            }
            update hal;
        }
        
    }
    
}